package cn.edu.hbpu.controller;

import cn.edu.hbpu.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("login")
public class LoginController {


    @Autowired
    private LoginService loginService;

    /**
     * @Author yuwenteng
     * @Date 2017/9/28 22:18
     * @Description 跳转登录页面
     */
    @RequestMapping("/showLoginPage")
    public String showLoginPage() {
        return "login";
    }

    /**
     * @Author yuwenteng
     * @Date 2017/10/12 13:50
     * @Description 校验并认证用户信息，跳转主页
     */
    @RequestMapping("/checkUserLoginInfo")
    @ResponseBody
    public String checkUserLoginInfo(String userName, String password, String checkValue) {
        //获取当前的subject对象
        Subject subject = SecurityUtils.getSubject();
        //判断当前用户是否已经被认证，即是否已经登录
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
            //判断是否使用记住我功能
            if (checkValue != null && checkValue.equals("rememberMe")) {
                token.setRememberMe(true);
            }
            try {
                subject.login(token);
                return "login_success";
            } catch (AuthenticationException ae) {
                return "login_error";
            }
        } else {
            return "login_success";
        }
    }

    /**
     * @Author yuwenteng
     * @Date 2017/9/28 22:19
     * @Description 校验教师用户登录信息，跳转主页
     */
    /*@RequestMapping("/checkTeacherLoginInfo")
    @ResponseBody
    public String checkTeacherLoginInfo(String userName, String password, HttpSession session) {
        //根据用户名查询用户信息
        Teacher teacher = loginService.getTeacherInfoByName(userName);
        if (teacher != null && teacher.getTeacherPassword().equals(password)) {
            //将用户信息存入session
            session.setAttribute("teaInfo", teacher);
            return "login_success";
        } else {
            return "login_error";
        }
    }*/
}




